package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.reggie.common.R;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 刘付磊
 * @date 2021/10/18 0018
 */
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @PostMapping
    public R<String> saveWithFlavor(@RequestBody DishDto dishDto) {
        log.info(dishDto.toString());
        dishService.saveWithFlavor(dishDto);
        return R.success("添加菜品成功");
    }

    @GetMapping("/page")
    public R<IPage<Dish>> page(String name, int page, int pageSize) {
        return dishService.page(name, page, pageSize);
    }

    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id) {

        DishDto byIdWithFlavor = dishService.getByIdWithFlavor(id);

        return R.success(byIdWithFlavor);
    }

    @PutMapping
    public R<String> upDate(@RequestBody DishDto dishDto) {
        dishService.upDateWithFlavor(dishDto);
        return R.success("修改成功");
    }

    /*@GetMapping("/list")
    public R<List<Dish>> list(Dish dish) {
        R<List<Dish>> list = dishService.list(dish);
        return list;
    }*/
    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){
        R<List<DishDto>> list = dishService.list(dish);
        return list;
    }
    @PostMapping("/status/*")
    public R<String> statusBy(String ids){
        dishService.statusBy(ids);
        return R.success("状态修改成功");
    }
    @DeleteMapping
    public R<String> dtIds(Long[] ids){
        log.info("dis={}", ids);
        R<String> dt = dishService.dtIds(ids);
        return dt;
    }
}
